from common.log import logger
from common.mymako import render_mako_context, render_json

from blueapps.account.decorators import login_exempt
from bk_test_systerm.models import test_tm_info
from django.shortcuts import render,redirect
from django.views.decorators.csrf import csrf_exempt

# Create your views here.
def home(request):
    """
    Home Page
    """
    return render(request, 'bk_test_systerm/test_home.html')
def home1(request):
    """
    Home Page
    """
    return render(request, 'bk_test_systerm/home.html')

def test_tm(request):
    return render(request, 'bk_test_systerm/test_tm.html')

@csrf_exempt
def delete_tm(request):
    ask_name = request.POST.get('ask_name', '')
    result = test_tm_info.objects.filter(ask_name=ask_name).delete()
    return render_json(result)

@csrf_exempt
def save_tm(request):
    ask_name = request.POST.get('ask_name', '')
    ans_A = request.POST.get('ans_A', '')
    ans_B = request.POST.get('ans_B', '')
    ans_C = request.POST.get('ans_C', '')
    ans_D = request.POST.get('ans_D', '')
    ans_sucess = request.POST.get('ans_sucess', '')
    ask_info = request.POST.get('ask_info', '')
    if not ask_name:
        result={'result': False, 'message': u"试题题目名称不能为空" }
    else :   
        data = {
            'ask_name': ask_name,
            'ans_A': ans_A,
            'ans_B': ans_B,
            'ans_C': ans_C,
            'ans_D': ans_D,
            'ans_sucess': ans_sucess,
            'ask_info': ask_info,
            'username': request.user.username,
        }
        result = test_tm_info.objects.save_tm(data)

    return render_json(result)

def records(request):
    record_list = test_tm_info.objects.all().order_by('-id')
    data = []
    for index, record in enumerate(record_list):
        data.append({
            'index': index,
            'ask_name': record.ask_name,
            'ans_A': record.ans_A,
            'ans_B': record.ans_B,
            'ans_C': record.ans_C,
            'ans_D': record.ans_D,
            'ans_sucess': record.ans_sucess,
            'ask_info': record.ask_info,
        })
    return render_json({'code': 0, 'message': 'success', 'data': data})   